package pzc.time;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * 程序运行时间计数工具类.
 * <p>
 * <pre>
 * 计算程序运行时间
 * 依赖第三方包：apache的commons-lang包
 * </pre>
 *
 * @author 彭
 * @version 1.0.0 2009/08/24
 */
public class TimeCounter {
	private Date startTime;
	private Date endTime;
	
	public void start(){
		this.startTime = new Date();
	}
	
	public void stop(){
		this.endTime = new Date();
	}
	
	public long getTotalMillSeconds(){
		return this.endTime.getTime() - startTime.getTime();
	}
	
	public void getSpandTime(){
		long totalMillSeconds = getTotalMillSeconds();
		long totalSeconds = totalMillSeconds / 1000;
		long millSeconds = totalMillSeconds % 1000;
		
		int days = (int)(totalSeconds / (24 * 60 * 60));
		totalSeconds = totalSeconds - days * (24 * 60 * 60);
		
		int hours = (int)((totalSeconds) / (60 * 60));
		totalSeconds = totalSeconds - hours * (60 * 60);
		
		int minutes = (int)((totalSeconds) / 60);
		int seconds = (int)(totalSeconds % 60);

		System.out.println("total seconds : " + totalSeconds + " s " + millSeconds +"ms");
		
		String strHours = StringUtils.leftPad(String.valueOf(hours), 2, "0");
		String strMinutes = StringUtils.leftPad(String.valueOf(minutes), 2, "0");
		String strSeconds = StringUtils.leftPad(String.valueOf(seconds), 2, "0");
		
		System.out.println(days + "日" + strHours + ":" + strMinutes + ":" + strSeconds + "." + + millSeconds +"ms");
		
	}
}
